package edu.cicese.alz.swing.tables;

import edu.cicese.alz.Subject;
import edu.cicese.alz.swing.SwingUtil;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import java.util.Map;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 29/05/12
 * Time: 01:02 PM
 */
public class UploadDSVTable extends JTable {

//	private final Color colorDelete = new Color(255, 209, 209);
//	private final Color colorInsert = new Color(201, 255, 193);
//	private final Font fontUpdate = new Font("Verdana", Font.ITALIC, 12);

	private Map<Integer, Subject> idxSubjects;

	public UploadDSVTable(Map<Integer, Subject> idxSubjects) {
		super();
		this.idxSubjects = idxSubjects;
	}


	@Override
	public boolean isCellEditable(int row, int col) {
		return col == 0 || !(col == 1 || col == 2) && idxSubjects.get(row).hasDSVFile(col - 3);
//
//		switch (col) {
//			case 1: case 2: return false;
//			case 3: //Accelerometer
//				return idxSubjects.get(row).hasAccelerometerDsv();
//			case 4: //GPS
//				return idxSubjects.get(row).hasGPSDsv();
//			case 5: //NFC
//				return idxSubjects.get(row).hasNFCDsv();
//			case 6: //WIFI
//				return idxSubjects.get(row).hasWIFIDsv();
//		}
//		return true;
	}

	@Override
	public TableCellRenderer getCellRenderer(int row, int col) {
		TableCellRenderer renderer = super.getCellRenderer(row, col);
		if (col == 1) {
			((JLabel) renderer).setHorizontalAlignment(SwingConstants.CENTER);
		}
		else if (col == 2) {
			((JLabel) renderer).setHorizontalAlignment(SwingConstants.LEFT);
		}
		return renderer;
	}

	@Override
	public Component prepareEditor(TableCellEditor editor, int row, int column) {
		Component c = super.prepareEditor(editor, row, column);

		setFont(c, row, column);
		
		return c;
	}

	@Override
	public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
		Component c = super.prepareRenderer(renderer, row, column);

		setFont(c, row, column);

		return c;
	}

	private void setFont(Component c, int row, int column) {
		c.setFont(SwingUtil.fontNormal);

		/*if (!isRowSelected(row)) {
			c.setBackground(getBackground());

			if (deletingRow(row)) {
				c.setBackground(colorDelete);
			}
			else if (insertingRow(row)) {
				c.setBackground(colorInsert);
			}
			else if (updatingRow(row)) {
				c.setFont(fontUpdate);
			}
		}*/

		if (column == 1) {
			c.setFont(SwingUtil.fontUserID);
		}
	}
}
